<?php
require '../../config.php';

$tablas = $_REQUEST['query'];

switch($tablas)
{
	case 1:
		{
			$tabla="categoriasclientes";
			break;
		}
	case 2:
		{
			$tabla="eventos";
			break;
		}
	case 3:
		{
			$tabla="expos";
			break;
		}
	case 4:
		{
			$tabla="provincias";
			break;
		}
	case 5:
		{
			$tabla="motivos";
			break;
		}
	case 6:
		{
			$tabla="tarifas";
			break;
		}
	case 7:
		{
			$tabla="formaspago";
			break;
		}
}





$resultset = mysql_query("select * from ".$tabla,$conexion) or die(mysql_error());

if(!$resultset)
{
    mysql_close();
    echo json_encode('error en el query: ' . mysql_error());
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
    echo "<span class='badge'>Tabla $tabla </span>&nbsp;&nbsp;<a href='#' <span class='glyphicon glyphicon-plus-sign' onclick='agregar(".$tablas.");'><strong>N</strong>uevo</span>";
    
}
else
{
		
			$numcampos = mysql_num_fields($resultset);
			$nombrecampo = "inicializado";
		
			echo   "<table class='table table-bordered'>";
			echo   "<caption><span class='badge'>Tabla $tabla </span>&nbsp;&nbsp;<a href='#' <span class='glyphicon glyphicon-plus-sign' onclick='agregar(".$tablas.");'><strong>N</strong>uevo</span></a></caption>";
			
			echo   "<tr>";
			for ($i=0;$i<$numcampos;$i++)
			{

				echo   "<th>".mysql_field_name($resultset, $i)."</th>";
				if($i==0) $nombrecampo = mysql_field_name($resultset, $i);
			}
			echo "<th>Opciones</th>";
			echo   "</tr>";
			echo   "<tbody>";
			$aux=0;
			
			while($row = mysql_fetch_row($resultset))
			{
				if($aux==0){
					$color="#FFFFFF";
					$aux=1;
				}else{
					$color="#F9F9F9";
					$aux=0;
				}
				echo ("<tr bgcolor='".$color."'>");
					for ($i=0;$i<count($row);$i++)
						{
							echo   "<td>"._convert($row[$i])."</td>";
						}
						
						echo   "<td><a href='#' <span class='glyphicon glyphicon-pencil'  onclick='modificar(".$row[0].",".$tablas.",\"".$nombrecampo."\");'></span></a> &nbsp;<a  href='#' <span class='glyphicon glyphicon-remove' onclick='eliminar(".$row[0].",".$tablas.",\"".$nombrecampo."\")'></span></a></td>";
				echo   ("</tr>");
			}
			
			echo   "</tbody>";
			echo   "</table>";
			mysql_close();
		
}


function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}

?>